package com.cloud.lawcase.api.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.lawcase.api.bean.ApiInterBean;
import com.cloud.lawcase.api.bean.LawApiSearchCriteria;
import com.cloud.lawcase.api.entity.TcAccessGlobalParams;
import com.cloud.lawcase.api.entity.TcAccessToken;
import com.cloud.lawcase.api.entity.TcAccesssInterface;
import com.cloud.lawcase.api.entity.TcRelTokenInterface;
import com.cloud.lawcase.api.service.LawApiManagerService;
import com.cloud.lawcase.common.result.Quokka;

import springfox.documentation.annotations.ApiIgnore;
@ApiIgnore
@RestController
@RequestMapping("lawcase/api/manager/api/")
public class LawApiManagerController {

	@Autowired
	public LawApiManagerService lawApiManagerService;

	@PostMapping("list")
	public Quokka listAppInfo(@RequestBody LawApiSearchCriteria searchCriteria) {
		return new Quokka(true, lawApiManagerService.listPage(searchCriteria));
	}

	@GetMapping("load")
	public Quokka load(@RequestParam(required = true) String appId) {
		return new Quokka(true, lawApiManagerService.load(appId));
	}

	@GetMapping("delete")
	public Quokka deleteApp(@RequestParam(required = true) String appId) {
		return new Quokka(true, lawApiManagerService.delete(appId));
	}

	@PostMapping("saveupdate")
	public Quokka saveUpdate(@RequestBody TcAccessToken token) {
		return new Quokka(true, lawApiManagerService.saveUpdate(token));
	}

	@GetMapping("listinters")
	public Quokka listSelectedInters(@RequestParam(required = true) String appId) {
		Quokka quokka = new Quokka(true);
		List<TcAccesssInterface> inters = lawApiManagerService.listAllInters();
		List<TcRelTokenInterface> rels = lawApiManagerService.listCheckedInters(appId);
		List<TcAccessGlobalParams> params = lawApiManagerService.listGlobalParams(appId);
		quokka.getDataMap().put("inters", inters);
		quokka.getDataMap().put("rels", rels);
		quokka.getDataMap().put("params", params);
		return quokka;
	}

	@PostMapping("saveinterrel")
	public Quokka saveInterRel(@RequestBody ApiInterBean apiInter) {
		lawApiManagerService.saveUpdateInterRel(apiInter);
		return new Quokka(true, apiInter.getAppId());
	}
}
